function toggle(hide) {
	var error = document.getElementById("error"); // error
	var legend = document.getElementById("progress_legend"); // legend
	
	if(hide){
		error.style.display = "block";
		legend.style.display = "none";
	} else {
		error.style.display = "none";
		legend.style.display = "block";
	}
} 

function load_dispatcher(url)
{
    selector = $("#selector").val();
	
    if(selector == undefined){
        selector = -1;
    }
	
    $("#the_content").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        selector:selector
    }, function(data) {
        $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
    }, "html");
}

function load_child_progress_report(child_id, class_id, level_id)
{
    var url = SITE_URL + '/progress_report/load_portfolio_report';
    var yearterm_id = $("#yearterm_selector").val();
	
    $("#progress_report").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        yearterm_id:yearterm_id,
        child_id:child_id,
        class_id:class_id,
        level_id:level_id
    }, function(data) {
        $("#progress_report").html(data).fadeIn();
    }, "html");
}

function load_progress_report(level_id, class_id)
{
    var url = SITE_URL + '/progress_report/load_progress_report';
    var yearterm_id = $("#yearterm_selector").val();
	
    // $("#the_content").html('');
    $("#the_content").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        yearterm_id:yearterm_id,
        class_id:class_id,
        level_id:level_id
    }, function(data) {
        // $("#the_waiting").html('');
        $("#the_content").html(data).fadeIn();
        get_progress_report(level_id, class_id);
    }, "html");
}

function get_progress_report(level_id, class_id)
{
	var yearterm_id = $("#yearterm_selector").val();
    var url = SITE_URL + '/progress_report/get_progress_report_title' + '/' + yearterm_id + '/' + class_id + '/' + level_id;
    
    $("#title").html('<img src = "../images/spinner.gif" />');
    //$("#title").html('');
    $.post(url, {}, function(data) {
        $("#title").html(data).fadeIn();
    }, "html");
   
   var url = SITE_URL + '/progress_report/get_progress_report_legend' + '/' + yearterm_id + '/' + class_id + '/' + level_id;
	$("#subject_legend_table").html('<img src = "../images/spinner.gif" />');
    $.post(url, {}, function(data) {
        $("#subject_legend_table").html(data).fadeIn();
    }, "html");
	
    url = SITE_URL + '/progress_report/get_progress_report';
    
    $("#data_table").html('');
    $("#the_waiting").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        yearterm_id:yearterm_id,
        class_id:class_id,
        level_id:level_id
    }, function(data) {
		toggle(data == '');
        $("#data_table").html(data).fadeIn();
        $("#the_waiting").html('');
    }, "html");
}

function load_child_portfolio(child_id, yearterm_id, level_id, class_id)
{
    var url = SITE_URL + '/progress_report/load_child_portfolio';
	
    $("#progress_report").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        child_id:child_id,
        yearterm_id:yearterm_id,
        class_id:class_id,
        level_id:level_id
    }, function(data) {
        $("#progress_report").html(data).fadeIn;
    }, "html");
}

function load_access_report(child_id, yearterm_id, level_id, class_id)
{
    var url = SITE_URL + '/progress_report/load_access_report';
	
    $("#progress_report").html('<img src = "../images/spinner.gif" />');
    $.post(url, {
        child_id:child_id,
        yearterm_id:yearterm_id,
        level_id:level_id,
        class_id:class_id
    }, function(data) {
        $("#progress_report").html(data).fadeIn;
    }, "html");
}

function update_child_access()
{
	if (!$("#child_info").validate().form())
		return;
	
    var o = $("#child_access").serializeArray();
	
    var item_arr = $(".exempt");
    var items = new Array();
	
    // var yearterm_id = $("#yearterm_id").val();
    // var child_id = $("#child_id").val();
    // var level_id = $("#level_id").val();
	
    //exemptions 
    for(i = 0; i < item_arr.length; i++){
        o.push({
            "name":"exempt[" + item_arr[i].value + "]",
            "value":(item_arr[i].checked?1:0)
        });
    }
	
    // rubric
    var name = ".selectedRubric";
    var selected_rubric = $(name);
    var mess = "";
    if(selected_rubric != undefined){
        for(i = 0; i < selected_rubric.length; i++){
            o.push({
                "name":selected_rubric[i].name,
                "value":(selected_rubric[i].checked?1:0)
            });
            mess = mess + selected_rubric[i].name + " = " + selected_rubric[i].checked + "\n";
        }
    }
	
    o.push({"name":"child_height","value":$("#child_height").val()});
    o.push({"name":"child_weight","value":$("#child_weight").val()});
	o.push({"name":"isApproved","value":($("#isApproved").is(':checked')?1:0)});

    // o.push({"name":"yeaterm_id","value":yearterm_id});
    // o.push({"name":"child_id","value":child_id});
    // o.push({"name":"level_id","value":level_id});
	
    var url = SITE_URL + '/progress_report/update_child_access';
	
    $("#progress_report").html('<img src = "../images/spinner.gif" />');
    $.post(url, o, function(data) {
        $("#progress_report").html(data).fadeIn;
    }, "html");
	
}

function submit_child_portfolio()
{
    var url = SITE_URL + '/progress_report/submit_child_portfolio';
    var o = $("#frm1").serializeArray();
    var year = $("#year").val(); 
    var term = $("#term").val(); 
    var child_id = $("#child_id").val(); 
    var yearterm_id = $("#yearterm_id").val(); 
    var level_id = $("#level_id").val(); 
    var class_id = $("#class_id").val(); 
	
    o.push({
        "name":"class_id",
        "value":class_id
    });
    o.push({
        "name":"year",
        "value":year
    });
    o.push({
        "name":"term",
        "value":term
    });
    o.push({
        "name":"child_id",
        "value":child_id
    });
    o.push({
        "name":"yearterm_id",
        "value":yearterm_id
    });
    o.push({
        "name":"level_id",
        "value":level_id
    });
	
    $("#progress_report").html('');
	$("#buttons").html('');
	
    $.post(url, o , function(data) {
        $("#progress_report").html(data).fadeIn;
    }, "html");
}

function doChild(level_id,sub_id){
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/children_profile/portfolio',
        data: 'level_id='+level_id+'&subject_id='+sub_id,
        success: function(data){
            if(data==1){
                location.href=SITE_URL + '/children_profile/index';
            }else{
                $('#the_content').html(data);
            }
        }
    })
}

function upload_artefact(lo_id,level_id,sub_id){
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/children_profile/artefact',
        data: 'lo_id='+lo_id+'&level_id='+level_id+'&subject_id='+sub_id,
        success: function(data){
            if(data==1){
                location.href=SITE_URL + '/children_profile/index';
            }else{
                $('#the_content').html(data);
            }
        }
    })
}

function doChildYear(){
    data = $('#changeyear').val();
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/children_profile/portfolio',
        data: data,
        success: function(data){
            $('#the_content').html(data);
        }
    })
}

function CheckExtenstion(con)
{
    if(con.value != "")
    {
        if(con.value.indexOf(".") > -1)
        {
            var ext = con.value.substring(con.value.lastIndexOf(".") + 1);
            if(ext.toLowerCase() == "jpeg" || ext.toLowerCase() == "jpg" || ext.toLowerCase() == "gif" || ext.toLowerCase() == "tif" || ext.toLowerCase() == "tiff" || ext.toLowerCase() == "bmp" || ext.toLowerCase() == "png" || ext.toLowerCase() == "wmv" || ext.toLowerCase() == "avi" || ext.toLowerCase() == "mpeg" || ext.toLowerCase() == "mpg" || ext.toLowerCase() == "mp4" || ext.toLowerCase() == "flv")
            {
            /* do nothing */
            /* find a better way of checking the file type */
            }
            else
            {
                alert('Please select an image or video file only.');
                con.value="";
                con.focus();
            }
        }
        else
        {
            alert('Please enter valid file');
        }
    }
}

function oneOrNoCheckboxGroup(checkbox)
{    
    var checkboxGroup = checkbox.value;
	
	for(i = 1; i <= 6; i++){
		var id = "selectedRubric_" + i + checkboxGroup;
		if(checkbox.id != id){
			$("#" + id).attr('checked',false);
		}
	}	
}
